package sfmc.biz.serviceImpl;

import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import sfmc.biz.dao.SfmcMainDao;
import sfmc.biz.service.SfmcMainService;
import sfmc.biz.vo.Sfmc_TB_MAIN;

@Service("SfmcMainService")
public class SfmcMainServiceImpl implements SfmcMainService{
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Resource(name="SfmcMainDao")
	private SfmcMainDao sfmcMainDao;
	
	public void setSfmcCustomer(SfmcMainDao sfmcMainDao){
		this.sfmcMainDao = sfmcMainDao;
	}

	public List<Sfmc_TB_MAIN> getMainList(Sfmc_TB_MAIN dto){
		return getMainList(dto, false);
	}
	public List<Sfmc_TB_MAIN> getMainList(Sfmc_TB_MAIN dto, boolean isExcel){
		
		String where = "";
		
		try{
			if(!"".equals(dto.getConfirmOrd())){
				if("1".equals(dto.getConfirmOrd())){
					where += "AND A.confirmOrd = '" + dto.getConfirmOrd() + "'\n";
				}else{
					where += "AND A.confirmOrd IS NULL \n";
				}
			}
			if(!"".equals(dto.getChannelNum())){
				where += "AND A.channelNum = '" + dto.getChannelNum() + "'\n";
			}
			if(!"".equals(dto.getSearchCallerID())){
				where += "AND A.callerID LIKE '%" + dto.getSearchCallerID() + "%'\n";
			}
			if(!"".equals(dto.getSearchCustName())){
				where += "AND B.custName LIKE '%" + dto.getSearchCustName() + "%'\n";
			}

			if(isExcel){
				where += "ORDER BY A.ordIdx DESC, A.ordIdx \n";
			}else{
				where += "ORDER BY A.ordIdx DESC, A.ordIdx LIMIT "+dto.getOffSet()+", "+dto.getPageSize()+" \n";
			}
			
			dto.setWhere(where);
			
			return sfmcMainDao.getMainList(dto);
			
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}

	@Override
	public int getRecordCount(Sfmc_TB_MAIN dto) {
	
		String where = "";
		
		try{
			if(!"".equals(dto.getConfirmOrd())){
				if("1".equals(dto.getConfirmOrd())){
					where += "AND A.confirmOrd = '" + dto.getConfirmOrd() + "'\n";
				}else{
					where += "AND A.confirmOrd IS NULL \n";
				}
			}
			if(!"".equals(dto.getChannelNum())){
				where += "AND A.channelNum = '" + dto.getChannelNum() + "'\n";
			}
			if(!"".equals(dto.getSearchCallerID())){
				where += "AND A.callerID LIKE '%" + dto.getSearchCallerID() + "%'\n";
			}
			if(!"".equals(dto.getSearchCustName())){
				where += "AND B.custName LIKE '%" + dto.getSearchCustName() + "%'\n";
			}
			if(!"".equals(where)){
				dto.setWhere(where);
			}
			return sfmcMainDao.getRecordCodunt(dto);
		}catch(Exception e){
			log.error(e);
			return 0;
		}
	}
	
	public int updateMain(Sfmc_TB_MAIN dto) {
		try{
			return sfmcMainDao.updateMain(dto);
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}

	public int deleteMain(Sfmc_TB_MAIN dto) {
		
		int returnVal = -1;
		
		try{
			if(!"".equals(dto.getDataStr())){
				
				StringTokenizer stk = new StringTokenizer(dto.getDataStr(),"|");
				
				while(stk.hasMoreTokens()){
					
					returnVal = sfmcMainDao.deleteMain(stk.nextToken());
					
					if(returnVal != 1){
						break;
					}
				}
			}
			
			return returnVal;
			
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}
}
